package com.lc.rongzecaifu.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lc.rongzecaifu.MyApplication;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.lc.rongzecaifu.ui.BorrowDetailActivity;
import com.zcx.helper.activity.AppV4Fragment;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/15.
 */
public class BorrowFragment extends AppV4Fragment implements View.OnClickListener {

    @BoundView(R.id.borrow_total_et) private EditText borrow_total_et;
    @BoundView(R.id.borrow_rate_et) private EditText borrow_rate_et;
    @BoundView(R.id.borrow_use_ll) private LinearLayout borrow_use_ll;
    @BoundView(R.id.borrow_use_tv) private TextView borrow_use_tv;
    @BoundView(R.id.borrow_duration_ll) private LinearLayout borrow_duration_ll;
    @BoundView(R.id.borrow_duration_tv) private TextView borrow_duration_tv;
    @BoundView(R.id.borrow_payback_ll) private LinearLayout borrow_payback_ll;
    @BoundView(R.id.borrow_payback_tv) private TextView borrow_payback_tv;
    @BoundView(R.id.borrow_payback_source_et) private TextView borrow_payback_source_et;
    @BoundView(R.id.tender_award_rg) private RadioGroup tender_award_rg;
    @BoundView(R.id.directional_rg) private RadioGroup directional_rg;
    @BoundView(R.id.fragment_borrow_next_bt) private Button fragment_borrow_next_bt;
    private PopupWindow mPop;
    private List<String> lists = new ArrayList<>(); //popwindow 数据源

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_borrow, null)));


        fragment_borrow_next_bt.setOnClickListener(this);
        borrow_use_ll.setOnClickListener(this);
        borrow_duration_ll.setOnClickListener(this);
        borrow_payback_ll.setOnClickListener(this);

        lists.add("jsjdfjasodfjosjfoi");
        lists.add(" 士大夫士大夫士大夫");
        lists.add("jsjdfj虽然我而味儿味儿asodfjosjfoi");
        lists.add("是沃尔沃而为二为");

        return view;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.fragment_borrow_next_bt:

                startActivity(new Intent(getActivity(), BorrowDetailActivity.class));

                break;

            case R.id.borrow_use_ll:

                closeKeyboard();

                initPop(borrow_use_tv, 0);

                break;

            case R.id.borrow_duration_ll:

                closeKeyboard();

                initPop(borrow_duration_tv, 1);

                break;

            case R.id.borrow_payback_ll:

                closeKeyboard();

                initPop(borrow_payback_tv, 2);

                break;
        }

    }

    public void initPop(final TextView mView, int popType) {

        if (mPop != null && mPop.isShowing()) {

            mPop.dismiss();

        } else {

            View view = getActivity().getLayoutInflater().inflate(R.layout.pop_menulist, null);

            ListView pop_lv = (ListView) view .findViewById(R.id.menulist);


            DisplayMetrics metric = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);

            int height = metric.heightPixels;

//            WindowManager wm = this.getWindowManager();
//            int height2 = wm.getDefaultDisplay().getHeight();
//            Log.e("---", "height" + height + "   height2 " + height2);

            int [] arr = new int[2];
            mView.getLocationOnScreen(arr);

            switch (popType) {

                case 0:
                    pop_lv.setAdapter(new ArrayAdapter<>(getActivity(), R.layout.spinner_list_item, lists));

                    break;
                case 1:
                    pop_lv.setAdapter(new ArrayAdapter<>(getActivity(), R.layout.spinner_list_item, lists));

                    break;
                case 2:
                    pop_lv.setAdapter(new ArrayAdapter<>(getActivity(), R.layout.spinner_list_item, lists));

                    break;
            }

//            Log.e("---", "weidth" + arr[0] + "height" + arr[1] + "bottom    " + borrow_manager_type_tv.getBottom());

            mPop = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, height - arr[1] - mView.getBottom(), true);

//            Log.e("---", "popheight" + mPop.getHeight());

            mPop.setTouchable(true);

            mPop.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return false;
                }
            });

            mPop.setBackgroundDrawable(new BitmapDrawable());

            mPop.showAsDropDown(mView);

            pop_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    mView.setText(lists.get(position));

                    switch (position) {

                        case 0:


                            break;

                        case 1:

                            break;

                        case 2:

                            break;

                        case 3:

                            break;

                    }

                    mPop.dismiss();
                }
            });


            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPop.dismiss();
                }
            });

        }
    }

    public void closeKeyboard() {

        InputMethodManager imm =  (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if(imm != null) {

            imm.hideSoftInputFromWindow(getActivity().getWindow().getDecorView().getWindowToken(), 0);
        }

    }
}
